package senior.SuperMarket;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author shkstart
 * @creat 2022-04-11:41
 */
public class SuperMarket {
    private String name;
    private String address;
    private Set<Customer> set = new HashSet<>();

    public SuperMarket() {
    }

    public SuperMarket(String name, String address) {
        this.name = name;
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return
                "名称：" + name + "\n地址：" + address;
    }

    /**
     * 开卡
     *
     * @param cust
     * @return
     */
    public boolean addCustomer(Customer cust) {
        if (set.contains(cust)) {
            return false;
        }
        set.add(cust);
        return true;
    }


    /**
     * 修改密码
     * @param no
     * @param pwd
     * @param newPwd
     * @return
     */
    public boolean modification(String no, String pwd,String newPwd) {
        Iterator<Customer> iterator = set.iterator();
        while (iterator.hasNext()) {
            Customer next = iterator.next();
            if(no.equals(next.getNo()) && pwd.equals(next.getPwd())){
               next.setPwd(newPwd);
               return true;
            }
        }


        return false;

    }

    /**
     * 兑换商品
     *
     * @param no
     * @param choose
     * @return
     */
    public String exchange(String no, int choose) {
        Iterator<Customer> iterator = set.iterator();
        while (iterator.hasNext()) {
            Customer next = iterator.next();
            int jifen = next.getJifen();
            if (no.equals(next.getNo())) {
                if (choose == 1) {
                    if (jifen >= 100) {
                        next.setJifen(jifen - 100);
                        return "成功兑换抽纸一提";
                    }
                    return "积分不足";
                } else if (choose == 2) {

                    if (jifen >= 1000) {
                        next.setJifen(jifen - 1000);
                        return "成功兑换电饭煲一台";
                    }
                    return "积分不足";
                } else if (choose == 3) {
                    if (jifen >= 10000) {
                        next.setJifen(jifen - 10000);
                        return "成功兑换洗衣机一台";
                    }
                    return "积分不足";
                }

            }

        }
        return "找不到该会员";


    }

    /**
     * 查询剩余积分
     *
     * @param no
     * @return -1代表没有该会员
     */
    public int inquire(String no) {
        Iterator<Customer> iterator = set.iterator();
        while (iterator.hasNext()) {
            Customer next = iterator.next();
            if (no.equals(next.getNo())) {
                return next.getJifen();
            }
        }
        return -1;
    }

}
